<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://doc.hyperf.io
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

namespace App\Controller;

use App\Model\Answer;
use App\Model\Com;
use App\Model\ComAn;
use App\Model\Result;
use App\Request\ComRequest;
use http\Env\Request;

class IndexController extends AbstractController
{
    public function show()
    {
        return $this->render->render('index.html');
    }

    /**
     * 新增公司
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function memberAdd()
    {
        $post = $this->request->post();
        foreach ($post as $key => $p) {
            if ($p === '') {
                return $this->response->json(['code' => 1, 'msg' => $key . '必填']);
            }
        }
        if (Com::query()->insert($post)) {
            $res = ['code' => 0];
        } else {
            $res = ['code' => 1, 'msg' => '没有写入'];
        }
        return $this->response->json($res);
    }

    /**
     * 查询
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function memberSearch()
    {
        $info = $this->request->input('mem');
        if ($info === '') {
            return $this->response->json(['code' => 1, 'msg' => '查询参数必填']);
        }
        $c = Com::query()->where('name', 'LIKE', $info . '%')->first();
        if($c){
            return $this->response->json(['code'=>0,'info'=>$c]);
        }else{
            return $this->response->json(['code'=>0,'info'=>Com::query()->where('tel', 'LIKE', $info . '%')->first()]);
        }
    }

    /**
     * 获取res
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function answerList()
    {
        return $this->response->json(['code'=>0,'info'=>Answer::all()]);
    }

    /**
     * 添加res
     * @return \Psr\Http\Message\ResponseInterface
     */
    public function answerAdd()
    {
        $info = $this->request->post();
        if(count($info)==2&&$info['type']&&$info['info']){

            if(Answer::query()->insert($info)){
                $res= ['code'=>0];
            }else{
                $res= ['code'=>1,'写入失败'];
            }

        }else{
            $res = ['code'=>1,'msg'=>'参数错误'];
        }
        return $this->response->json($res);
    }

    /**
     * 新增结果
     * @return []
     */
    public function resultAdd()
    {
        $info = $this->request->post();
        $insert = [];
        $time = time();
        foreach ($info['interest'] as $t=>$item){
            $insert[] = [
                'cid'=>$info['cid'],
                'aid'=>$t,
                'result'=>$info['done'],
                'create_time'=>$time
            ];
        }
        foreach ($info['excuse'] as $t=>$item){
            $insert[] = [
                'cid'=>$info['cid'],
                'aid'=>$t,
                'result'=>$info['done'],
                'create_time'=>$time
            ];
        }
        ComAn::query()->insert($insert);
        return ['code'=>0];
    }

}
